import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import base from "@src/store/base";
import { queryDiscount } from "@src/api/order";
import router from "@src/router";
import { discountMd5 } from "@src/common/discountUtils";
import {debounce, throttle} from "@src/common/utils";
import { checkPwdInit, getBaseInfo } from "@src/api/member";

Component({
  options: {
    addGlobalClass: true
  },
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['themeColor', 'themeTitleColor'],
  }],
  properties: {
    accountPayRule: {
      type: Object
    },
    product: {
      type: Object
    },
    payRule: {
      type: Object
    },

    num: {
      type: Number
    },
    priceList: {
      type: Array,
    },
    beginDay: {
      type: String,
    },
  },
  data: {
    hasInit: false,
    totalFee: 0,
    price: 0,
    discountInfo: null,
    discountFee: 0
  },
  observers: {
    'num': function () {
      this.computeFee();
    },
    'beginDay': function (beginDay) {
      if (beginDay) {
        this.computeFee();
      }
    },
    'totalFee': function (totalFee) {
      if (!totalFee) {
        this.changeDiscount(null);
      } else {
        this.getDiscountInfo();
      }
    }
  },
  lifetimes: {
    ready: function () {
      this.computeFee()
      this.setData({ hasInit: true })
    }
  },
  methods: {
    getDiscountInfo() {
      if (!this.data.payRule.canUseDiscount) {
        return;
      }
      let totalFee = this.data.totalFee;
      if (totalFee === 0) {
        return 0;
      }
      let data = {
        orderType: 7,
        shopId: this.data.product.shopId,
        tradeFee: totalFee
      }
      queryDiscount(data).then(res => {
        if (res.code === 0) {
          if (this.data.totalFee === totalFee && res.data.length > 0) {
            this.changeDiscount(res.data[0]);
          }
        }
      })
    },
    changeDiscount(discountInfo) {
      if (discountInfo) {
        this.setData({ discountInfo: discountInfo, discountFee: discountInfo.discountFee })
      } else {
        this.setData({ discountInfo: null, discountFee: 0 })
      }
    },
    computeFee() {
      let totalFee = 0;
      let priceList = this.data.priceList;
      let beginDay = this.data.beginDay;
      let num = this.data.num
      if (this.data.product.productType === 0) {
        for (let i = 0; i < priceList.length; i++) {
          totalFee = totalFee + num * priceList[i].price;
        }
        this.setData({ totalFee: totalFee })
      } else if (this.data.beginDay) {
        for (let i = 0; i < priceList.length; i++) {
          if (priceList[i].day === beginDay) {
            totalFee = num * priceList[i].price;
            let price = num * priceList[i].price
            this.setData({ totalFee: totalFee, price })
            break;
          }
        }
      }
    },
    handleShowDetail() {
      this.setData({ dialogShow: true })
    },
    handleHideDetail() {
      this.setData({ dialogShow: false })
    },
    async handleSubmit() {
      let isPwdInit = false
      const { data:{ memberCategoryId } } = await getBaseInfo();
      if(memberCategoryId) {
        const { data:{ initialed } } = await checkPwdInit();
        isPwdInit = initialed
      } 
      const { accountPayRule, totalFee, discountFee} = this.data
      let price = totalFee - discountFee
      if(memberCategoryId && price > accountPayRule.skipPwdAmount && !isPwdInit) {
        router.push({ name: "member/resetPassword", params: { type: "init" } })
        return
      }
      const that =this;
      throttle(() => {
        let data = {
          arrivalTime: that.data.arrivalTime,
          payFee: that.data.totalFee - that.data.discountFee > 0 ? that.data.totalFee - that.data.discountFee : 0,
          discountInfo: that.data.discountInfo
        }
        that.triggerEvent('submit', data)
      }, 1000)
    },
    handleChangeDiscount() {
      let discountInfo = this.data.discountInfo;
      let selectItem = discountMd5(discountInfo)
      router.push({
        name: 'selectDiscount',
        params: {
          shopId: this.data.product.shopId,
          totalFee: this.data.totalFee,
          orderType: 7,
          selectItem: selectItem
        },
        events: {
          'change': (data) => {
            this.changeDiscount(data)
          }
        }
      })
    }
  }
});

